//
//  MCPopupViewController.m
//  MClient
//
//  Created by  史正烨 on 11-2-18.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MCPopupViewController.h"
#import "MCUIToolkit.h"

@implementation MCPopupViewController

#pragma mark -
#pragma mark View lifecycle

- (id)init {
	if (self = [super init]) {
		self.hidesBottomBarWhenPushed = YES;
		_backView = [[UIImageView alloc] initWithImage:[MCUIToolkit copyScreen]];
        if (nil != _backView) {
			CGRect frame = CGRectMake(0, -20, self.width, self.height+20);
			_backView.frame = frame;
		}

		//_foreView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height+20, self.width, self.height+20)];
        _foreView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height+20)];
		_foreView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
		[_backView addSubview:_foreView];
	}
	return self;
}

- (BOOL) isPortrait {
	return UIInterfaceOrientationIsPortrait(self.appDelegate.navController.interfaceOrientation);
}

- (void)viewDidLoad {
    [super viewDidLoad];

	
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.rightBarButtonItem = self.editButtonItem;
	if (_backView) {
		[self.view addSubview:_backView];
		[self.view sendSubviewToBack:_backView];
	}
	
	_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.height, self.width, 300) 
											  style:UITableViewStyleGrouped];
	_tableView.backgroundColor = [UIColor clearColor];
	_tableView.scrollEnabled = NO;
	_tableView.delegate = self;
	_tableView.dataSource = self;
	[self.view addSubview:_tableView];
}

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	
	self.navigationController.navigationBarHidden = YES;
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 3;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
    
	cell.textLabel.text = [NSString stringWithFormat:@"cell at section=%d,row=%d", indexPath.section, indexPath.row];
    // Configure the cell...
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 38.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
	return 44.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
	return 44.0;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	/*
	 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
	 [self.navigationController pushViewController:detailViewController animated:YES];
	 [detailViewController release];
	 */
	[self close];
}

- (void)dealloc {
	[_foreView release];
	[_backView release];
	[_tableView release];
    [super dealloc];
}

- (void)show:(UINavigationController*)naviController
{
	if ([naviController.visibleViewController respondsToSelector:@selector(keyboardDidHidden:)]) {
		[naviController.visibleViewController performSelector:@selector(keyboardDidHidden:) withObject:nil];
	}
	
	[naviController pushViewController:self animated:NO];

	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.3];
	_foreView.frame = CGRectMake(0, 0, self.width, self.height+20);
	_tableView.frame = [self tableViewLocation];
	[UIView commitAnimations]; 
}

- (void)close
{
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDidStopSelector:@selector(popoutView)];
	[UIView setAnimationDuration:0.3];
	_foreView.frame = CGRectMake(0, CGRectGetHeight(_foreView.frame), CGRectGetWidth(_foreView.frame), CGRectGetHeight(_foreView.frame));
	_tableView.frame = CGRectMake(0, self.height, CGRectGetWidth(_tableView.frame), CGRectGetHeight(_tableView.frame));
	[UIView commitAnimations]; 
}

-(void)popoutView {
	self.navigationController.navigationBarHidden = NO;
	[self.navigationController popViewControllerAnimated:NO];
}

- (CGRect)tableViewLocation {
	return CGRectMake(0, self.isPortrait ? 150 : 70, self.width, 200);
}

- (float)width {
	return self.isPortrait ? CGRectGetWidth(self.view.bounds) : CGRectGetHeight(self.view.bounds);
}
- (float)height {
	return self.isPortrait ? CGRectGetHeight(self.view.bounds) : CGRectGetWidth(self.view.bounds);
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return NO;
}
@end

